package com.example.mongodbdemo.services;

import java.util.List;
import java.util.Optional;

import com.example.mongodbdemo.entity.User;
import com.example.mongodbdemo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImp implements UserService {
   @Autowired
   UserRepository userRepository;

   public void createUser(User user) {
      // TODO Auto-generated method stub
      userRepository.save(user);
   }

   public List<User> getUser() {
      // TODO Auto-generated method stub
      return (List<User>) userRepository.findAll();
   }

   public Optional<User> findById(String id) {
      // TODO Auto-generated method stub
      return userRepository.findById(id);
   }

   public User update(User user, String l) {
      // TODO Auto-generated method stub
      return userRepository.save(user);
   }

   public void deleteUserById(String id) {
      // TODO Auto-generated method stub
      User user = new User();
      user.setId(id);
      userRepository.delete(user);
   }

   public User updatePartially(User user, String id) {
      // TODO Auto-generated method stub
      Optional<User> userOptional = findById(id);
      userOptional.get().setCountry(user.getCountry());
      return userRepository.save(userOptional.get());
   }
}